#!/usr/bin/python2.5
import hildon
import gtk
import gobject
import sys
import traceback

from .log import log
from .gui import app
from .gui.app import GuiApp
from current_song import CurrentSong
import mainmenu
import screensaver

from .podcast import db
from .podcast import subscriptions
from .podcast import download

fixed = None
window = None

def CreateUiComponents(window, app):
  current_song = CurrentSong()
  mainmenu.Create(app, current_song)
  app.SetCurrentScreen(mainmenu.id, GuiApp.ENTER_FROM_LEFT)

def InitUi():
  global fixed
  global window

  window = hildon.Window()
  window.fullscreen()
  window.connect("destroy", gtk.main_quit)

  fixed = gtk.Fixed()
  window.add(fixed)

  app = GuiApp(fixed)

  window.show_all()
  app.show()
  CreateUiComponents(window, app)

def InitPodcasts():
  db.Init()

  subscriptions.Load()
  download.Start()
  #subscriptions.ImportOPML("podcast/podcasts.opml")

def Run():
  try:
    gobject.threads_init()
    log("main: Call initpodcasts")
    InitPodcasts()
    log("main: call initui")
    InitUi()
    log("main: call screensaver.Start()")
    screensaver.Start()
    log("main: call gtk.main()")
    gtk.main()
    log("Gtk.main returned.")
  except KeyboardInterrupt:
    log("^C")
    sys.exit(1)
  except:
    log("Exception in main thread")
    traceback.print_exc()
    sys.exit(1)
  log("Terminating.")
  sys.exit(1)

